//  ------------ 测试产生虚拟数据 -------------------

const _materialDataList = [];

const _itemDataList = [];

const _ItemBomList=[];

//  ------------ 赋值 ------------------------
new Vue({
    el: '#app',
    data: function() {
        return {
            activeName: 'first',
            materialDataDialog:[],
            materialSearchDialog : false,
            itemBomSearchDialog : false,
            ItemBomData: _ItemBomList,
            qtyRestrictList: [{
                label: "仅1.0",
                value: "仅1.0"
            }, {
                label: "任意数字",
                value: "任意数字"
            }, {
                label: "整数",
                value: "整数"
            }],
            typeList:[{
                label : "制作",
                value : "制作"
            },{
                label : "制作/采购",
                value : "制作/采购"
            },{
                label : "采购",
                value : "采购"
            },{
                label : "安装",
                value : "安装"
            }],
            orderTypeList:[{
                label : "工程",
                value : "工程"
            },{
                label : "检验",
                value : "检验"
            },{
                label : "返工",
                value : "返工"
            },{
                label : "生产",
                value : "生产"
            },{
                label : "重复",
                value : "重复"
            },{
                label : "退回物料授权",
                value : "退回物料授权"
            },{
                label : "工具管理",
                value : "工具管理"
            },{
                label : "备件",
                value : "备件"
            },{
                label : "安装",
                value : "安装"
            }],
            statusList:[{
                label : "可下达",
                value : "可下达"
            },{
                label : "新建",
                value : "新建"
            },{
                label : "作废",
                value : "作废"
            },{
                label : "冻结",
                value : "冻结"
            },{
                label : "保留",
                value : "保留"
            }],
            selectGroup:[],
            materialForm:{
                material:'',
                version:'',
                description:'',
                status:'',
                currentVersion:'',
                type:'',
                lotSize:'',
                orderType:'',
                unitOfMeasure:'',
                routerVersion:'',
                router:'',
                bomVersion:'',
                bom:'',
                quantityRestriction:'',
                qtyMultiplier:''
            }
        }
    },
    methods: {
        query(){
            var me = this;
            var material = me.materialForm.material;
            var quid = document.getElementById("materialHidden").value;
            if( quid==="" ){
                this.$notify({
                    title: '消息',
                    message: '请先选择物料!'
                });
            }else{
                var param = {
                    serviceName:"materialConfigService",
                    serviceMethodName:"getMaterialList",
                    id:"",
                    quid:quid,
                    plant:"TEST"
                };
                function callBack( data ){
                    me._materialDataList=[];
                    var _form = me.materialForm;
                    var materialList = data.retList;
                    if(materialList.length>0){
                        var materialObj = materialList[0];
                        _form.status=materialObj.statusFk;
                        _form.currentVersion=materialObj.curVersion==="Y"?true:false;
                        _form.type=materialObj.matTypeFk;
                        _form.lotSize=materialObj.lotSize;
                        _form.orderType=materialObj.srcStatusFk;
                        _form.unitOfMeasure=materialObj.measureUnit;
                        _form.routerVersion=materialObj.routerFk;
                        _form.router=materialObj.routerFk;
                        _form.bomVersion=materialObj.bomFk;
                        _form.bom=materialObj.bomFk;
                        _form.quantityRestriction=materialObj.qtyLimit;
                        _form.qtyMultiplier=materialObj.baseUnit;
                    }
                };
                me.ajaxGetRequest( param,callBack );
            }
        },
        saveMaterial(){
            var me = this;
            var _form = me.materialForm;
            var hiddenBomFk = document.getElementById("hiddenMaterialQuid").value;
            var lotBatch = _form.lotSize;
            if(hiddenBomFk===""){
                me.$notify({
                    title: '消息',
                    message: '请选择物料清单!'
                });
            }else if(lotBatch===""){
                me.$notify({
                    title: '消息',
                    message: '请填写批次大小!'
                });
            }else{
                var param = {
                    serviceName:"materialConfigService",
                    serviceMethodName:"saveMaterialData",
                    curVersion:_form.currentVersion?"Y":"N",
                    id:_form.material,
                    name:_form.description,
                    plant:"TEST",
                    bomFk:hiddenBomFk,
                    matTypeFk:_form.type,
                    version:_form.version,
                    measureUnit:_form.unitOfMeasure,
                    lotSize:_form.lotSize,
                    srcStatusFk:_form.orderType,
                    routerFk:_form.router,
                    qtyLimit:_form.quantityRestriction,
                    baseUnit:_form.qtyMultiplier,
                    statusFk:_form.status
                };
                function callBack( data ){
                    if(date.retCode==="1"){
                        me.$notify({
                            title: '消息',
                            message: '保存成功!'
                        });
                    }
                };
                me.ajaxGetRequest( param,callBack );
            }
        },
        deleteMaterial(){
            var me = this;
            var quid = document.getElementById("materialHidden").value;
            if(quid===""){
                me.$notify.error({
                    title: '错误',
                    message: '请先执行查询！'
                });
            }else{
                var param = {
                    serviceName:"materialConfigService",
                    serviceMethodName:"deleteMaterial",
                    quid:quid
                };
                function callBack( data ){
                    if(date.retCode==="1"){
                        me.$notify.info({
                            title: '消息',
                            message: '删除成功!'
                        });
                    }
                };
                me.ajaxGetRequest( param,callBack );
            }
        },
        materialSearch(){
            var me = this;
            var material = me.materialForm.material;
            var param = {
                serviceName:"materialConfigService",
                serviceMethodName:"getMaterialList",
                id:material,
                quid:"",
                plant:"TEST"
            };
            function callBack( data ){
                if( data.retList.length >0 ){
                    me.materialDataDialog = data.retList;
                }else{
                    me.materialDataDialog = [];
                }
                me.materialSearchDialog = true;
            };
            me.ajaxGetRequest( param,callBack );
        },
        itemBomSearch(){
            var me = this;
            var id = me.materialForm.bom;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"bomListByBom",
                plant:"TEST",
                id:id,
                version:""
            };
            function callBack( data ){
                me._ItemBomList=[];
                var bomList = data.retObj.BomList;
                if(bomList.length>0){
                    for(var i=0;i<bomList.length;i++){
                        _ItemBomList.push({
                            "quid": bomList[i].quid,
                            "id": bomList[i].id,
                            "name":bomList[i].name,
                            "version": bomList[i].version
                        });
                    }
                }
                me.itemBomSearchDialog = true;
            };
            me.ajaxGetRequest( param,callBack );
        },
        ajaxPostRequest( params, callBackFun )
        {
            var me = this, _url = "http://localhost:8082/route";
            axios({
                method: 'post',
                url: _url,
                params: params
            }).then(function(response){
                if( response.status == 200 ){
                    callBackFun( response.data );
                }else{
                    console.log( response );
                }
            }).catch(function(err){
                console.log(err);
            });
        },
        ajaxGetRequest(  params, callBackFun ){
            var me = this, _url = "http://localhost:8082/route";
            axios.get( _url,{
                params: params
            }).then(function(response){
                if( response.status == 200 ){
                    callBackFun( response.data );
                }else{
                    console.log( response );
                }
            }).catch(function(err){
                console.log(err);
            });
        },
        selectedRowMaterial(row, event) {
            var bom = row.bom;
            var version = row.version;
            var id = row.id;
            var name = row.name;
            var quid = row.quid;
            this.materialSearchDialog=false;
            this.materialForm.material = id;
            this.materialForm.version = version;
            this.materialForm.description = name;
            document.getElementById("materialHidden").value = quid;
        },
        selectedRowBom(row, event) {
            var bom = row.bom;
            var version = row.version;
            var id = row.id;
            var name = row.name;
            var quid = row.quid;
            this.itemBomSearchDialog=false;
            this.materialForm.bom = id;
            this.materialForm.bomVersion = version;
            document.getElementById("hiddenMaterialQuid").value = quid;
        },
        // --------------------------------------------
        filterMethod() {
            console.log('submit!');
        },
        handleClick(tab, event) {
            console.log(tab, event);
        },
        clearMaterial(){
            this.materialForm.version="";
            this.materialForm.lotSize="";
            this.materialForm.unitOfMeasure="";
            this.materialForm.routerVersion="";
            this.materialForm.bomVersion="";
            this.materialForm.bom="";
            this.materialForm.material="";
            this.materialForm.quantityRestriction="";
            this.materialForm.qtyMultiplier="";
            this.materialForm.description="";
            document.getElementById("materialHidden").value="";
        },
        handleIconClick(ev) {
            console.log(ev);
        },
        checkChange(obj,isChecked,children){
            if(isChecked){
                this.$refs.bomTree.setCheckedKeys([]);
                this.$refs.bomTree.setCheckedKeys([obj.id]);
            }
        },
        remove() {
            var mTree = this.$refs.bomTree.getCheckedNodes();
            if( mTree.length>0 ){
                /*for(var i=0;i<mTree.length;i++){
                 var node = mTree[i];
                 }*/
            }
        }
    }
})